﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.CommonObject;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.ManageWms
{
    public partial class StockGoodsStatWnd : UserControl
    {
        #region 成员变量
        private PagerIndex _pagerIndex;
        private StatStockView _queryRecord;
        #endregion

        #region 构造函数
        public StockGoodsStatWnd()
        {
            InitializeComponent();
            InitBtnImage();

            this.tbsWarehouse.Tag = MainForm.CurrentUser.WarehouseCode;
            this.tbsWarehouse.SetTextBoxContent(MainForm.CurrentUser.WarehouseName);
            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                this.tbsWarehouse.SetEditState(true);
            }
            else
            {
                this.tbsWarehouse.SetEditState(false);
            }
            ResetDataPager();
            _queryRecord = new StatStockView();
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void LoadQueryResult(StatStockView record, ref PagerIndex pIndex)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<StatStockView> queryList = client.QueryStatStockList(record, ref pIndex);
                dataPager.RecordCount = pIndex.RecordCount;
                this.statStockViewBindingSource.DataSource = null;
                this.statStockViewBindingSource.DataSource = queryList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        #endregion

        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            ResetDataPager();
            _pagerIndex.CurrentPageIndex = 1;
            _queryRecord = new StatStockView();
            if (tbGoodsCode.Text.Trim() != "")
            {
                _queryRecord.Id = this.tbGoodsCode.Text.Trim();
            }
            if (tbGoodsName.Text.Trim() != "")
            {
                _queryRecord.GoodsName = this.tbGoodsName.Text.Trim();
            }
            this.LoadQueryResult(_queryRecord, ref _pagerIndex);
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            this.LoadQueryResult(_queryRecord, ref _pagerIndex);
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }
        #endregion

    }
}
